package vip.xiaonuo.supplier.modular.finance.balancelog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.finance.balancelog.entity.SupplierBalanceLog;
import vip.xiaonuo.supplier.modular.finance.balancelog.mapper.SupplierBalanceLogMapper;
import vip.xiaonuo.supplier.modular.finance.balancelog.param.SupplierBalanceLogAddParam;
import vip.xiaonuo.supplier.modular.finance.balancelog.param.SupplierBalanceLogEditParam;
import vip.xiaonuo.supplier.modular.finance.balancelog.param.SupplierBalanceLogIdParam;
import vip.xiaonuo.supplier.modular.finance.balancelog.param.SupplierBalanceLogPageParam;
import vip.xiaonuo.supplier.modular.finance.balancelog.service.SupplierBalanceLogService;

import java.util.List;

/**
 * 账户日志Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/05/17 11:43
 **/
@Service
public class SupplierBalanceLogServiceImpl extends ServiceImpl<SupplierBalanceLogMapper, SupplierBalanceLog> implements SupplierBalanceLogService {

    @Override
    public Page<SupplierBalanceLog> page(SupplierBalanceLogPageParam supplierBalanceLogPageParam) {
        QueryWrapper<SupplierBalanceLog> queryWrapper = new QueryWrapper<SupplierBalanceLog>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierBalanceLogPageParam.getLogType())) {
            queryWrapper.lambda().eq(SupplierBalanceLog::getLogType, supplierBalanceLogPageParam.getLogType());
        }
        if(ObjectUtil.isNotEmpty(supplierBalanceLogPageParam.getLogAccountId())) {
            queryWrapper.lambda().eq(SupplierBalanceLog::getLogAccountId, supplierBalanceLogPageParam.getLogAccountId());
        }
        if(ObjectUtil.isNotEmpty(supplierBalanceLogPageParam.getLogBusinessId())) {
            queryWrapper.lambda().eq(SupplierBalanceLog::getLogBusinessId, supplierBalanceLogPageParam.getLogBusinessId());
        }
        if(ObjectUtil.isNotEmpty(supplierBalanceLogPageParam.getLogBusinessCode())) {
            queryWrapper.lambda().eq(SupplierBalanceLog::getLogBusinessCode, supplierBalanceLogPageParam.getLogBusinessCode());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierBalanceLog::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierBalanceLogPageParam.getSortField(), supplierBalanceLogPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierBalanceLogPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierBalanceLogPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierBalanceLogPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierBalanceLog::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierBalanceLogAddParam supplierBalanceLogAddParam) {
        SupplierBalanceLog supplierBalanceLog = BeanUtil.toBean(supplierBalanceLogAddParam, SupplierBalanceLog.class);
        //数据归属
        supplierBalanceLog.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierBalanceLog);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierBalanceLogEditParam supplierBalanceLogEditParam) {
        SupplierBalanceLog supplierBalanceLog = this.queryEntity(supplierBalanceLogEditParam.getId());
        BeanUtil.copyProperties(supplierBalanceLogEditParam, supplierBalanceLog);
        this.updateById(supplierBalanceLog);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierBalanceLogIdParam> supplierBalanceLogIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierBalanceLogIdParamList, SupplierBalanceLogIdParam::getId));
    }

    @Override
    public SupplierBalanceLog detail(SupplierBalanceLogIdParam supplierBalanceLogIdParam) {
        return this.queryEntity(supplierBalanceLogIdParam.getId());
    }

    @Override
    public SupplierBalanceLog queryEntity(String id) {
        SupplierBalanceLog supplierBalanceLog = this.getById(id);
        if(ObjectUtil.isEmpty(supplierBalanceLog)) {
            throw new CommonException("账户日志不存在，id值为：{}", id);
        }
        return supplierBalanceLog;
    }
}
